//
//  MWPasswordLoginController.m
//  XiRan-iOS
//
//  Created by Richfit on 2019/10/21.
//  Copyright © 2019 Wood. All rights reserved.
//

#import "MWPasswordLoginController.h"
#import "MWForgetPasswordController.h"
#import "MWPhoneLoginController.h"
#import "MWRegisterController.h"
#import "MWImprovedInfoController.h"

#import "AppDelegate.h"
#import "MWTabBarController.h"
#import "MWXRTabBarController.h"

#import <FBSDKCoreKit/FBSDKCoreKit.h>
#import <FBSDKLoginKit/FBSDKLoginKit.h>


#import "HSFTabBarController.h"

#import "MWNavigationController.h"
#import "MWHomeController.h"
#import "ShowRoomViewController.h"
#import "MWShopController.h"
#import "MWTreeSeedController.h"
#import "MWApplicationController.h"
#import "MWMineController.h"

#define kIs_iphone (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
#define kIs_iPhoneX kScreenWidth >=375.0f && kScreenHeight >=812.0f&& kIs_iphone
/*TabBar高度*/
#define kTabBarHeight (CGFloat)(kIs_iPhoneX?(49.0 + 34.0):(49.0))


@interface MWPasswordLoginController () <MWWXApiManagerDelegate>

@property(nonatomic, strong) UIButton *backBtn;
@property(nonatomic, strong) UILabel *hintLB;
@property(nonatomic, strong) UITextField *phoneTF;
@property(nonatomic, strong) UIView *phoneLineView;
@property(nonatomic, strong) UITextField *passwordTF;
@property(nonatomic, strong) UIView *passwordLineView;

@property(nonatomic, strong) UIButton *loginBtn;
@property(nonatomic, strong) UIButton *captchaLogitnBtn;
@property(nonatomic, strong) UIButton *forgetPwdBtn;
@property(nonatomic, strong) UIButton *registerBtn;
@property(nonatomic, strong) UIButton *fbLoginBtn;
@property(nonatomic, strong) UIButton *wxLoginBtn;

@end

@implementation MWPasswordLoginController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.hidden = YES;
    [self setupPageSubviews];
    [self layoutPageSubviews];
    [self setLocalizable];
    [MWWXApiManager sharedManager].delegate = self;
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleDefault;
}

- (void)backAction:(UIButton *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)loginAction:(UIButton *)sender {
    [self.view endEditing:YES];
    [self loginWithPhone:_phoneTF.text password:_passwordTF.text];
}

- (void)captchaLoginAction:(UIButton *)sender {
    MWPhoneLoginController *controller = [[MWPhoneLoginController alloc] init];
    [self.navigationController pushViewController:controller animated:YES];
}

- (void)forgetPwdAction:(UIButton *)sender {
    MWForgetPasswordController *controller = [[MWForgetPasswordController alloc] init];
    [self.navigationController pushViewController:controller animated:YES];
}

- (void)registerAction:(UIButton *)sender {
    MWRegisterController *controller = [[MWRegisterController alloc] init];
    [self.navigationController pushViewController:controller animated:YES];
}

#pragma mark - Facebook Action
- (void)facebookLoginAction:(UIButton *)sender {
    FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
    [login logInWithPermissions: @[@"public_profile"] fromViewController:self handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
      if (error) {
        DLog(@"Failer");
          
      } else if (result.isCancelled) {
        DLog(@"Cancelled");
          
      } else {
          [self loginWithFaceBook:result.token.userID];
      }
    }];
}

#pragma mark - FaceBook登录
- (void)loginWithFaceBook:(NSString *)fackbookId {
    NSMutableDictionary *paraDic = [[NSMutableDictionary alloc] init];
    [paraDic setObject:fackbookId forKey:@"fackbook_id"];
    [paraDic setObject:[MWConfigManager manager].productType forKey:@"is_material"];
    [paraDic setObject:[MWConfigManager manager].language forKey:@"language"];
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[UIApplication sharedApplication].keyWindow animated:YES];
    hud.label.text = [[MWConfigManager manager] objectForKey:@"loadin" defaultValue:@"加载中..."];
    
    [[MWNetworkManager manager] POST:kFacebookLoginUrl parameters:paraDic progress:^(NSProgress *downloadProgress) {
        
    } success:^(id responseObject) {
        NSDictionary *dic = responseObject;
        NSInteger code = [[dic objectForKey:@"code"] integerValue];
        NSString *msg = [dic objectForKey:@"msg"];
        
        if (code == 1) {
            [hud hideAnimated:YES];
            NSDictionary *dataDic = [dic objectForKey:@"data"];
            NSInteger status = [[dataDic objectForKey:@"status"] integerValue];
            if (status == 0) {
                MWImprovedInfoController *controller = [[MWImprovedInfoController alloc] init];
                controller.fackbookId = [dataDic objectForKey:@"facebook"];
                [self.navigationController pushViewController:controller animated:YES];
                
            } else {
                MWUser *user = [MWUser mj_objectWithKeyValues:[dataDic objectForKey:@"userinfo"]];
                [MWUserManager manager].user = user;
                [[MWUserManager manager] saveUser];

                if ([[MWConfigManager manager].productType isEqualToString:@"0"]) {
                    NSString *homeTitle = [[MWConfigManager manager] objectForKey:@"homepage" defaultValue:@"首页"];
                    NSString *showroomTitle = [[MWConfigManager manager] objectForKey:@"showroom" defaultValue:@"展厅"];
                    NSString *shopTitle = [[MWConfigManager manager] objectForKey:@"goods" defaultValue:@"商品"];
                    NSString *treeSeedTitle = [[MWConfigManager manager] objectForKey:@"treetype" defaultValue:@"树种"];
                    NSString *applicationTitle = [[MWConfigManager manager] objectForKey:@"application" defaultValue:@"应用"];
                    NSString *mineTitle = [[MWConfigManager manager] objectForKey:@"my" defaultValue:@"我的"];

                    MWNavigationController *navi1 = [[MWNavigationController alloc] initWithRootViewController:[[MWHomeController alloc] init]];
                    MWNavigationController *navi2 = [[MWNavigationController alloc] initWithRootViewController:[[ShowRoomViewController alloc] init]];
                    MWNavigationController *navi3 = [[MWNavigationController alloc] initWithRootViewController:[[MWShopController alloc] init]];
                    MWNavigationController *navi4 = [[MWNavigationController alloc] initWithRootViewController:[[MWTreeSeedController alloc] init]];
                    MWNavigationController *navi5 = [[MWNavigationController alloc] initWithRootViewController:[[MWApplicationController alloc] init]];
                    MWNavigationController *navi6 = [[MWNavigationController alloc] initWithRootViewController:[[MWMineController alloc] init]];
                    HSFTabBarController *rootController = [[HSFTabBarController alloc] init];
                    rootController.childVCArr = @[navi1, navi2, navi3, navi5, navi4, navi6];
                    rootController.source = @[@{@"title":homeTitle, @"selImg":@"tab_home_s", @"norImg":@"tab_home_n"},
                    @{@"title":showroomTitle, @"selImg":@"tab_showroom_s", @"norImg":@"tab_showroom_n"},
                    @{@"title":shopTitle, @"selImg":@"tab_shop_s", @"norImg":@"tab_shop_n"},
                                @{@"title":applicationTitle, @"selImg":@"tab_application_s", @"norImg":@"tab_application_n"},
                                              @{@"title":treeSeedTitle, @"selImg":@"tab_tree_seed_s", @"norImg":@"tab_tree_seed_n"},

                    @{@"title":mineTitle, @"selImg":@"tab_mine_s", @"norImg":@"tab_mine_n"}];
                    rootController.norColor = MWHexColor(@"#666666");
                    rootController.selColor = MWHexColor(@"#333333");
                    rootController.loadAll = YES;//是否一次全部加载
                    rootController.canScroll = NO;
                    rootController.tabBarHeight = kTabBarHeight;
                    rootController.style = HSFTabBarStyle_baseline;
                    rootController.baseline_color = MWHexColor(@"#333333");
                    rootController.isHaveTopline = NO;
                    [rootController setUp];
                    UIApplication *application = [UIApplication sharedApplication];
                    application.keyWindow.rootViewController = rootController;
                    [MWUserManager manager].tbc = rootController;

                } else {
                    MWXRTabBarController *controller = [[MWXRTabBarController alloc] init];
                    UIApplication *application = [UIApplication sharedApplication];
                    application.keyWindow.rootViewController = controller;
                }
            }
            
        } else {
            hud.mode = MBProgressHUDModeText;
            hud.label.text = msg;
            [hud hideAnimated:YES afterDelay:2];
        }
    } failure:^(NSError *error) {
        [hud hideAnimated:YES];
    }];
}

#pragma mark - 微信登录Action
- (void)weixinLoginAction:(UIButton *)sender {
    //构造SendAuthReq结构体
    SendAuthReq *req = [[SendAuthReq alloc] init];
    req.scope = @"snsapi_userinfo";
    req.state = @"Wood";
    //第三方向微信终端发送一个SendAuthReq消息结构
    [WXApi sendReq:req completion:^(BOOL success) {
        
    }];
}

#pragma mark - MWWXApiManagerDelegate
- (void)managerDidRecvAuthResponse:(SendAuthResp *)response {
    if (response.errCode == 0) {
        [self loadWXAccessTokenWithCode:response.code];
    }
}

#pragma mark 微信登录根据code获取access_token
- (void)loadWXAccessTokenWithCode:(NSString *)code {
    NSMutableDictionary *paraDic = [[NSMutableDictionary alloc] init];
    [paraDic setObject:WXAppID forKey:@"appid"];
    [paraDic setObject:WXAppSecret forKey:@"secret"];
    [paraDic setObject:code forKey:@"code"];
    [paraDic setObject:@"authorization_code" forKey:@"grant_type"];
    
    NSString *url = @"https://api.weixin.qq.com/sns/oauth2/access_token";
    
    [[MWNetworkManager manager] POST:url parameters:paraDic progress:^(NSProgress *downloadProgress) {
        
    } success:^(id responseObject) {
        NSDictionary *dic = responseObject;
        [self loginWithOpenid:[dic objectForKey:@"openid"]];
        
    } failure:^(NSError *error) {

    }];
}

#pragma mark - 微信登录
- (void)loginWithOpenid:(NSString *)openid {
    NSMutableDictionary *paraDic = [[NSMutableDictionary alloc] init];
    [paraDic setObject:openid forKey:@"openid"];
    [paraDic setObject:[MWConfigManager manager].productType forKey:@"is_material"];
    [paraDic setObject:[MWConfigManager manager].language forKey:@"language"];
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[UIApplication sharedApplication].keyWindow animated:YES];
    hud.label.text = [[MWConfigManager manager] objectForKey:@"loadin" defaultValue:@"加载中..."];
    
    [[MWNetworkManager manager] POST:kWXLoginUrl parameters:paraDic progress:^(NSProgress *downloadProgress) {
        
    } success:^(id responseObject) {
        NSDictionary *dic = responseObject;
        NSInteger code = [[dic objectForKey:@"code"] integerValue];
        NSString *msg = [dic objectForKey:@"msg"];
        
        if (code == 1) {
            [hud hideAnimated:YES];
            NSDictionary *dataDic = [dic objectForKey:@"data"];
            NSInteger status = [[dataDic objectForKey:@"status"] integerValue];
            if (status == 0) {
                MWImprovedInfoController *controller = [[MWImprovedInfoController alloc] init];
                controller.openid = [dataDic objectForKey:@"openid"];
                [self.navigationController pushViewController:controller animated:YES];
                
            } else {
                MWUser *user = [MWUser mj_objectWithKeyValues:[dataDic objectForKey:@"userinfo"]];
                [MWUserManager manager].user = user;
                [[MWUserManager manager] saveUser];

                if ([[MWConfigManager manager].productType isEqualToString:@"0"]) {
                    NSString *homeTitle = [[MWConfigManager manager] objectForKey:@"homepage" defaultValue:@"首页"];
                    NSString *showroomTitle = [[MWConfigManager manager] objectForKey:@"showroom" defaultValue:@"展厅"];
                    NSString *shopTitle = [[MWConfigManager manager] objectForKey:@"goods" defaultValue:@"商品"];
                    NSString *treeSeedTitle = [[MWConfigManager manager] objectForKey:@"treetype" defaultValue:@"树种"];
                    NSString *applicationTitle = [[MWConfigManager manager] objectForKey:@"application" defaultValue:@"应用"];
                    NSString *mineTitle = [[MWConfigManager manager] objectForKey:@"my" defaultValue:@"我的"];

                    MWNavigationController *navi1 = [[MWNavigationController alloc] initWithRootViewController:[[MWHomeController alloc] init]];
                    MWNavigationController *navi2 = [[MWNavigationController alloc] initWithRootViewController:[[ShowRoomViewController alloc] init]];
                    MWNavigationController *navi3 = [[MWNavigationController alloc] initWithRootViewController:[[MWShopController alloc] init]];
                    MWNavigationController *navi4 = [[MWNavigationController alloc] initWithRootViewController:[[MWTreeSeedController alloc] init]];
                    MWNavigationController *navi5 = [[MWNavigationController alloc] initWithRootViewController:[[MWApplicationController alloc] init]];
                    MWNavigationController *navi6 = [[MWNavigationController alloc] initWithRootViewController:[[MWMineController alloc] init]];
                    HSFTabBarController *rootController = [[HSFTabBarController alloc] init];
                    rootController.childVCArr = @[navi1, navi2, navi3, navi5, navi4, navi6];
                    rootController.source = @[@{@"title":homeTitle, @"selImg":@"tab_home_s", @"norImg":@"tab_home_n"},
                    @{@"title":showroomTitle, @"selImg":@"tab_showroom_s", @"norImg":@"tab_showroom_n"},
                    @{@"title":shopTitle, @"selImg":@"tab_shop_s", @"norImg":@"tab_shop_n"},
                                @{@"title":applicationTitle, @"selImg":@"tab_application_s", @"norImg":@"tab_application_n"},
                                              @{@"title":treeSeedTitle, @"selImg":@"tab_tree_seed_s", @"norImg":@"tab_tree_seed_n"},

                    @{@"title":mineTitle, @"selImg":@"tab_mine_s", @"norImg":@"tab_mine_n"}];
                    rootController.norColor = MWHexColor(@"#666666");
                    rootController.selColor = MWHexColor(@"#333333");
                    rootController.loadAll = YES;//是否一次全部加载
                    rootController.canScroll = NO;
                    rootController.tabBarHeight = kTabBarHeight;
                    rootController.style = HSFTabBarStyle_baseline;
                    rootController.baseline_color = MWHexColor(@"#333333");
                    rootController.isHaveTopline = NO;
                    [rootController setUp];
                    UIApplication *application = [UIApplication sharedApplication];
                    application.keyWindow.rootViewController = rootController;
                    [MWUserManager manager].tbc = rootController;

                } else {
                    MWXRTabBarController *controller = [[MWXRTabBarController alloc] init];
                    UIApplication *application = [UIApplication sharedApplication];
                    application.keyWindow.rootViewController = controller;
                }
            }
            
        } else {
            hud.mode = MBProgressHUDModeText;
            hud.label.text = msg;
            [hud hideAnimated:YES afterDelay:2];
        }
    } failure:^(NSError *error) {
        [hud hideAnimated:YES];
    }];
}


#pragma mark - 登录请求
- (void)loginWithPhone:(NSString *)phone password:(NSString *)password {
    NSMutableDictionary *paraDic = [[NSMutableDictionary alloc] init];
    [paraDic setObject:phone forKey:@"account"];
    [paraDic setObject:password forKey:@"password"];
    [paraDic setObject:[MWConfigManager manager].productType forKey:@"is_material"];
    [paraDic setObject:[MWConfigManager manager].language forKey:@"language"];
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[UIApplication sharedApplication].keyWindow animated:YES];
    hud.label.text = [[MWConfigManager manager] objectForKey:@"loadin" defaultValue:@"加载中..."];
    
    [[MWNetworkManager manager] POST:kLoginUrl parameters:paraDic progress:^(NSProgress *downloadProgress) {
        
    } success:^(id responseObject) {
        NSDictionary *dic = responseObject;
        NSInteger code = [[dic objectForKey:@"code"] integerValue];
        NSString *msg = [dic objectForKey:@"msg"];
        if (code == 1) {
            [hud hideAnimated:YES];
            NSDictionary *dataDic = [dic objectForKey:@"data"];
            MWUser *user = [MWUser mj_objectWithKeyValues:[dataDic objectForKey:@"userinfo"]];
            [MWUserManager manager].user = user;
            [[MWUserManager manager] saveUser];
            
            [[NSNotificationCenter defaultCenter] postNotificationName:kLoginResultNotification object:nil];
            self.navigationController.navigationBarHidden = NO;
            [self.navigationController popToRootViewControllerAnimated:YES];
            
        } else {
            hud.mode = MBProgressHUDModeText;
            hud.label.text = msg;
            [hud hideAnimated:YES afterDelay:2];
        }
        
    } failure:^(NSError *error) {
        [hud hideAnimated:YES];
    }];
}

- (void)setLocalizable {
    if ([[MWConfigManager manager].productType isEqualToString:@"0"]) {
        _hintLB.text = [[MWConfigManager manager] objectForKey:@"welcome_into_wood" defaultValue:@"欢迎来到我的木头"];
    } else {
        _hintLB.text = [[MWConfigManager manager] objectForKey:@"welcome_into_xiran" defaultValue:@"欢迎来到木头猎人"];
    }
    _phoneTF.placeholder = [[MWConfigManager manager] objectForKey:@"input_account" defaultValue:@"请输入账号"];
    _passwordTF.placeholder = [[MWConfigManager manager] objectForKey:@"input_pwd" defaultValue:@"请输入密码"];
    [_captchaLogitnBtn setTitle:[[MWConfigManager manager] objectForKey:@"login_by_code" defaultValue:@"验证码登录"] forState:UIControlStateNormal];
    [_forgetPwdBtn setTitle:[[MWConfigManager manager] objectForKey:@"forget_pwd" defaultValue:@"忘记密码"] forState:UIControlStateNormal];
    [_loginBtn setTitle:[[MWConfigManager manager] objectForKey:@"login" defaultValue:@"登录"] forState:UIControlStateNormal];
    
    NSString *no_account = [[MWConfigManager manager] objectForKey:@"no_account" defaultValue:@"还没有账号？"];
    NSString *register_now = [[MWConfigManager manager] objectForKey:@"register_now" defaultValue:@"立即注册"];
    
    NSString *registerHint = [NSString stringWithFormat:@"%@%@",no_account, register_now];
    NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:registerHint attributes: @{NSFontAttributeName: MWFont(13),NSForegroundColorAttributeName: MWHexColor(@"#666666")}];
    [string addAttributes:@{NSForegroundColorAttributeName: MWHexColor(@"#666666")} range:NSMakeRange(0, no_account.length)];
    [string addAttributes:@{NSForegroundColorAttributeName: MWHexColor(@"#FD9F2B")} range:NSMakeRange(no_account.length, register_now.length)];
    [_registerBtn setAttributedTitle:string forState:UIControlStateNormal];
    
    [_wxLoginBtn setTitle:[[MWConfigManager manager] objectForKey:@"wx" defaultValue:@"微信"]  forState:UIControlStateNormal];
    [_wxLoginBtn setIconInTopWithSpacing:10];
}

- (void)setupPageSubviews {
    _backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_backBtn setImage:[UIImage imageNamed:@"ic_arrow_left"] forState:UIControlStateNormal];
    [_backBtn setImage:[UIImage imageNamed:@"ic_arrow_left"] forState:UIControlStateHighlighted];
    [_backBtn addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_backBtn];
    
    _hintLB = [[UILabel alloc] init];
    _hintLB.font = MWBoldFont(22);
    _hintLB.numberOfLines = 2;
    _hintLB.textColor = MWHexColor(@"#1E1934");
    [self.view addSubview:_hintLB];
    
    _phoneTF = [[UITextField alloc] init];
    _phoneTF.font = MWFont(14);
    _phoneTF.textColor = MWHexColor(@"#BAC2CE");
    [self.view addSubview:_phoneTF];
    
    _phoneLineView = [[UIView alloc] init];
    _phoneLineView.backgroundColor = MWHexColor(@"#D8D8D8");
    [self.view addSubview:_phoneLineView];
    
    _passwordTF = [[UITextField alloc] init];
    _passwordTF.secureTextEntry = YES;
    _passwordTF.font = MWFont(14);
    _passwordTF.textColor = MWHexColor(@"#BAC2CE");
    [self.view addSubview:_passwordTF];
    
    _passwordLineView = [[UIView alloc] init];
    _passwordLineView.backgroundColor = MWHexColor(@"#D8D8D8");
    [self.view addSubview:_passwordLineView];

    
    _loginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _loginBtn.layer.cornerRadius = 5;
    _loginBtn.layer.masksToBounds = YES;
    _loginBtn.titleLabel.font = MWFont(14);
    [_loginBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_loginBtn setBackgroundImage:[UIImage imageWithColor:MWHexColor(@"#1E1934")] forState:UIControlStateNormal];
    [_loginBtn addTarget:self action:@selector(loginAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_loginBtn];
    
    _captchaLogitnBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _captchaLogitnBtn.titleLabel.font = MWFont(12);
    [_captchaLogitnBtn setTitleColor:MWHexColor(@"#666666") forState:UIControlStateNormal];
    [_captchaLogitnBtn addTarget:self action:@selector(captchaLoginAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_captchaLogitnBtn];
    
    _forgetPwdBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _forgetPwdBtn.titleLabel.font = MWFont(12);
    [_forgetPwdBtn setTitleColor:MWHexColor(@"#E53030") forState:UIControlStateNormal];
    [_forgetPwdBtn addTarget:self action:@selector(forgetPwdAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_forgetPwdBtn];
    
    _registerBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _registerBtn.titleLabel.font = MWFont(13);
    [_registerBtn addTarget:self action:@selector(registerAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_registerBtn];
    
    _fbLoginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _fbLoginBtn.hidden = YES;
    _fbLoginBtn.titleLabel.font = MWFont(12);
    [_fbLoginBtn setTitle:@"facebook" forState:UIControlStateNormal];
    [_fbLoginBtn setTitleColor:MWHexColor(@"#C2C6CD") forState:UIControlStateNormal];
    [_fbLoginBtn setImage:[UIImage imageNamed:@"login_fb"] forState:UIControlStateNormal];
    [_fbLoginBtn addTarget:self action:@selector(facebookLoginAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_fbLoginBtn];
    
    _wxLoginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _wxLoginBtn.hidden = YES;
    _wxLoginBtn.titleLabel.font = MWFont(12);
    [_wxLoginBtn setTitleColor:MWHexColor(@"#C2C6CD") forState:UIControlStateNormal];
    [_wxLoginBtn setImage:[UIImage imageNamed:@"login_wx"] forState:UIControlStateNormal];
    [_wxLoginBtn addTarget:self action:@selector(weixinLoginAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_wxLoginBtn];
    
//    if ([WXApi isWXAppInstalled] && [WXApi isWXAppSupportApi]) {
//        _wxLoginBtn.hidden = NO;
//
//    } else {
//        _wxLoginBtn.hidden = YES;
//    }
}

- (void)layoutPageSubviews {
    [_backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(kStatusBarHeight + 15);
        make.left.equalTo(@16);
    }];
    
    [_hintLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(kStatusBarHeight + 110);
        make.left.equalTo(self.view).offset(16);
        make.right.mas_lessThanOrEqualTo(-12);
    }];
    
    [_phoneTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.hintLB.mas_bottom).offset(48);
        make.left.equalTo(self.view).offset(16);
        make.right.equalTo(self.view).offset(-16);
    }];
    
    [_phoneLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.phoneTF.mas_bottom).offset(6);
        make.left.equalTo(self.view).offset(16);
        make.right.equalTo(self.view).offset(-16);
        make.height.equalTo(@0.5);
    }];
    
    [_passwordTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.phoneLineView.mas_bottom).offset(23);
        make.left.equalTo(self.view).offset(16);
        make.right.equalTo(self.view).offset(-16);
    }];
    
    
    [_passwordLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.passwordTF.mas_bottom).offset(6);
        make.left.equalTo(self.view).offset(16);
        make.right.equalTo(self.view).offset(-16);
        make.height.equalTo(@0.5);
    }];
    
    [_captchaLogitnBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.passwordLineView.mas_bottom).offset(8);
        make.left.equalTo(self.view).offset(16);
    }];
    
    [_forgetPwdBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.passwordLineView.mas_bottom).offset(8);
        make.right.equalTo(self.view).offset(-16);
    }];
    
    [_loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.passwordLineView.mas_bottom).offset(107);
        make.left.equalTo(self.view).offset(16);
        make.right.equalTo(self.view).offset(-16);
        make.height.equalTo(@44);
    }];
    
    [_registerBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.loginBtn.mas_bottom).offset(16);
        make.centerX.equalTo(self.view);
    }];
    
    [_fbLoginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view).offset(-90);
        make.centerX.equalTo(self.view).offset(-45);
    }];
    [_fbLoginBtn setIconInTopWithSpacing:10];
    
    [_wxLoginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view).offset(-90);
        make.centerX.equalTo(self.view).offset(45);
    }];
    
    [_wxLoginBtn setIconInTopWithSpacing:10];
    
}


@end
